package com.woniuxy.b_realm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @author lg
 * @time 2020/07/04 11:35:37
 */
public class App {

    public static void main(String[] args) {
      // 安全管理器
        DefaultSecurityManager securityManager=new DefaultSecurityManager ();
        //  创建 realm
        MyRealm realm=new MyRealm ();
         //  此时，securityManager也知道了。 知道有哪些账户，以及账户的密码是啥了。
        securityManager.setRealm (realm);
        // 将 securityManager 设置成全局
        SecurityUtils.setSecurityManager (securityManager);
         //获取主体 主题就是 于当前系统进行交互的那个东东
        Subject subject = SecurityUtils.getSubject ();
         // 进行认证  令牌
        UsernamePasswordToken token=new UsernamePasswordToken ("abc","111");

        try {
            subject.login (token); //间接的调用MyReleam的认证方法
            System.out.println ("success");
        } catch (AuthenticationException e) {
            System.out.println ("failure");
            e.printStackTrace ();
        }

        //授权
        System.out.println ("user:save" + subject.isPermitted ("user:save"));
        System.out.println("user:delete " + subject.isPermitted("user:delete"));
        System.out.println("user:update " + subject.isPermitted("user:update"));
        System.out.println("user:find " + subject.isPermitted("user:find"));

    }

}
